package com.talking.activities

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Typeface
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.util.Base64
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import com.google.gson.Gson
import com.huawei.hms.framework.common.Utils
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsBuildBitmapOption
import com.huawei.hms.ml.scan.HmsScan
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions
import com.parfoismeng.slidebacklib.registerSlideBack
import com.parfoismeng.slidebacklib.unregisterSlideBack
import com.talking.App
import com.talking.BuildConfig
import com.talking.R
import com.talking.bean.UserBean
import com.talking.databinding.ActivityQrcodeBinding
import com.talking.util.Tools
import java.io.File

class QrcodeActivity : AppCompatActivity() {
    private lateinit var binding: ActivityQrcodeBinding
    private lateinit var qrbitmap: Bitmap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       binding=ActivityQrcodeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.qrScanTip.typeface= Typeface.createFromAsset(assets, "font1.TTF")
        qrbitmap=ScanUtil.buildBitmap("talking"+Base64.encodeToString((intent.getSerializableExtra("user") as UserBean).toString().toByteArray(), Base64.DEFAULT), HmsScan.QRCODE_SCAN_TYPE, 400, 400, HmsBuildBitmapOption.Creator().setBitmapBackgroundColor(Color.WHITE).setBitmapColor(Color.rgb(240, 99, 101)).create())
        binding.qrIv.setImageBitmap(qrbitmap)
        binding.qrScan.setOnClickListener {
            ScanUtil.startScan(this, 3, HmsScanAnalyzerOptions.Creator().setHmsScanTypes(HmsScan.ALL_SCAN_TYPE).create())
        }
        binding.qrShare.setOnClickListener {
            Tools.saveBitmap(externalCacheDir.toString(), "qrcode.png",qrbitmap)
            val file = File(externalCacheDir.toString() + "/qrcode.png")
            val share = Intent(Intent.ACTION_SEND)
            share.type = "image/*"
            share.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                share.putExtra(
                    Intent.EXTRA_STREAM,
                    FileProvider.getUriForFile(
                        this,
                        BuildConfig.APPLICATION_ID + ".fileprovider",
                        file
                    )
                )
                share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            } else {
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file))
            }
            startActivity(Intent.createChooser(share,"分享到："))
        }
        registerSlideBack { finish() }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 3 && data != null) {
            val obj: Any? = data.getParcelableExtra(ScanUtil.RESULT)
            if (obj is HmsScan) {
                val value = obj.getOriginalValue()
                if (!TextUtils.isEmpty(value)) {
                    if (value.contains("talking")){
                        val other = Gson().fromJson(String(Base64.decode(value.replace("talking","").toByteArray(), Base64.DEFAULT)), UserBean::class.java)
                        Log.i("emmmmm", "onActivityResult: $other")



                    }else{
                        Tools.toast(getString(R.string.only_scan_talking))
                    }
                }
            }
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        unregisterSlideBack()
    }
}